home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / FROMUTS / UNIXLIB37B / src / unix / c / dev < prev    next >
Text File  |  1992-02-18  |  5KB  |  270 lines

  1. static char sccs_id[] = "@(#) dev.c 2.0 "__DATE__" HJR";
  2.  
  3. /* dev.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <errno.h>
  6. #include <stdlib.h>
  7.  
  8. #include "fcntl.h"
  9.  
  10. #include "sys/unix.h"
  11. #include "sys/param.h"
  12. #include "sys/dev.h"
  13. #include "sys/os.h"
  14.  
  15. struct dev __dev[NDEV] =
  16.   {
  17.     { __fsopen,__fsclose,__fsread,__fswrite,__fslseek,__fsioctl },
  18.     { __ttyopen,__ttyclose,__ttyread,__ttywrite,__ttylseek,__ttyioctl },
  19.     { __pipeopen,__pipeclose,__piperead,__pipewrite,__pipelseek,__pipeioctl },
  20.     { __nullopen,__nullclose,__nullread,__nullwrite,__nulllseek,__nullioctl }
  21.   };
  22.  
  23. int __fsopen(char *file,int mode,struct file *f)
  24. {
  25. register int oflag = f->oflag;
  26. register int *r = f->r;
  27. os_error *e;
  28. int fd;
  29.  
  30. file = __uname(file,oflag & (O_CREAT|O_OMASK));
  31.  
  32. if (e = os_file(0x05,file,r))
  33.   {
  34.   __seterr(e);
  35.   return(-1);
  36.   }
  37.  
  38. if (r[0])
  39.   {
  40.   if ((oflag & (O_EXCL|O_CREAT)) == (O_EXCL|O_CREAT))
  41.     {
  42.     errno = EEXIST;
  43.     return(-1);
  44.     }
  45.   if (oflag & O_CREAT) oflag &= ~O_CREAT;
  46.   if (oflag & O_OMASK)
  47.     if (e = os_file(0x09,file,r))
  48.       {
  49.       __seterr(e);
  50.       return(-1);
  51.       }
  52.   }
  53. else
  54.   {
  55.   if ((oflag & (O_RDONLY|O_CREAT)) == O_RDONLY)
  56.     {
  57.     errno = ENOENT;
  58.     return(-1);
  59.     }
  60.   if (oflag & O_OMASK) oflag |= O_CREAT;
  61.   }
  62.  
  63. if (oflag & O_CREAT)
  64.   {
  65.   r[2] = (oflag & O_BINARY) ? 0xffd : 0xfff; r[4] = r[5] = 0;
  66.   if (e = os_file(0x0b,file,r))
  67.     {
  68.     __seterr(e);
  69.     return(-1);
  70.     }
  71.  
  72.   mode &= ~(__u->umask & 0777);
  73.  
  74.   r[5] =  ((mode & 0400)>>8) | ((mode & 0200)>>6) |
  75.     ((mode & 0004)<<2) | ((mode & 0002)<<4);
  76.  
  77.   if (e = os_file(0x04,file,r))
  78.     {
  79.     __seterr(e);
  80.     return(-1);
  81.     }
  82.   }
  83.  
  84. if (e = os_file(0x05,file,r))
  85.   {
  86.   __seterr(e);
  87.   return(-1);
  88.   }
  89.  
  90. if (oflag & O_TRUNC)
  91.   e = os_fopen(0x80,file,&fd);
  92. else
  93.   switch (oflag & O_OMASK)
  94.     {
  95.     case O_RDONLY:
  96.       e = os_fopen(0x40,file,&fd);
  97.       break;
  98.     case O_WRONLY:
  99.     case O_RDWR:
  100.       e = os_fopen(0xc0,file,&fd);
  101.       break;
  102.     default:
  103.       errno = EINVAL;
  104.       return(-1);
  105.       break;
  106.     }
  107.   
  108. if (e)
  109.   {
  110.   __seterr(e);
  111.   return(-1);
  112.   }
  113.  
  114. return(fd);
  115. }
  116.  
  117. int __fsclose(int fd,struct file *f)
  118. {
  119. os_error *e;
  120.  
  121. if (e = os_fclose(fd))
  122.   {
  123.   __seterr(e);
  124.   return(-1);
  125.   }
  126.  
  127. return(0);
  128. }
  129.  
  130. int __fsread(int fd,register void *data,register int nbyte,struct file *f)
  131. {
  132. int r[5];
  133. os_error *e;
  134.  
  135. if (e = os_fread(fd,data,nbyte,r))
  136.   {
  137.   __seterr(e);
  138.   return(-1);
  139.   }
  140.  
  141. return(nbyte - r[3]);
  142. }
  143.  
  144. int __fswrite(int fd,register void *data,register int nbyte,struct file *f)
  145. {
  146. int r[5];
  147. os_error *e;
  148.  
  149. if (e = os_fwrite(fd,data,nbyte,r))
  150.   {
  151.   __seterr(e);
  152.   return(-1);
  153.   }
  154.  
  155. return(nbyte - r[3]);
  156. }
  157.  
  158. long __fslseek(int fd,long lpos,int whence,struct file *f)
  159. {
  160. int r[3];
  161. os_error *e;
  162.  
  163. switch (whence)
  164.   {
  165.   case 0:
  166.     if (e = os_args(1,fd,(int)lpos,r))
  167.       {
  168.       __seterr(e);
  169.       return(-1);
  170.       }
  171.     break;
  172.   case 1:
  173.     if (e = os_args(0,fd,0,r))
  174.       {
  175.       __seterr(e);
  176.       return(-1);
  177.       }
  178.     if (e = os_args(1,fd,r[2] + (int)lpos,r))
  179.       {
  180.       __seterr(e);
  181.       return(-1);
  182.       }
  183.     break;
  184.   case 2:
  185.     if (e = os_args(2,fd,0,r))
  186.       {
  187.       __seterr(e);
  188.       return(-1);
  189.       }
  190.     if (e = os_args(1,fd,r[2] + (int)lpos,r))
  191.       {
  192.       __seterr(e);
  193.       return(-1);
  194.       }
  195.     break;
  196.   default:
  197.     errno = EINVAL;
  198.     return(-1);
  199.     break;
  200.   }
  201.  
  202. return((long)r[2]);
  203. }
  204.  
  205. int __fsioctl(int fd,register int request,void *arg,struct file *f)
  206. { errno = EINVAL; return(-1); }
  207.  
  208. struct pipe *__pipe;
  209.  
  210. int __pipeopen(char *file,int mode,struct file *f)
  211. {
  212. return (__fsopen(file,mode,f));
  213. }
  214.  
  215. int __pipeclose(int fd,struct file *f)
  216. {
  217. struct pipe *pi = __pipe,*pi_ = 0;
  218.  
  219. while (pi)
  220.   {
  221.   if (pi->p[0] == f || pi->p[1] == f) break;
  222.   pi_ = pi; pi = pi->next;
  223.   }
  224. if (!pi) { errno = EBADF; return(-1); }
  225. if (pi_) pi_->next = pi->next; else __pipe = pi->next;
  226. __fsclose(fd,f);
  227.   {
  228.   os_error *e;
  229.  
  230.   if (e = os_fsctrl(27,__uname(pi->file,0),0,0642))
  231.     { __seterr(e); return(-1); }
  232.   }
  233. free(pi->file);
  234. free(pi);
  235. return(0);
  236. }
  237.  
  238. int __piperead(int fd,void *data,int nbyte,struct file *f)
  239. {
  240. if (f->oflag & O_PIPE)
  241.   {
  242.   f->oflag &= ~O_PIPE;
  243.   __fslseek(fd,0,0,f);
  244.   }
  245. return(__fsread(fd,data,nbyte,f));
  246. }
  247.  
  248. int __pipewrite(int fd,void *data,int nbyte,struct file *f)
  249. {
  250. if (f->oflag & O_PIPE)
  251.   return(__fswrite(fd,data,nbyte,f));
  252. else
  253.   { errno = EPIPE; return(-1); }
  254. }
  255.  
  256. long __pipelseek(int fd,long lpos,int whence,struct file *f)
  257. { errno = ESPIPE; return(-1); }
  258.  
  259. int __pipeioctl(int fd,register int request,void *arg,struct file *f)
  260. { errno = EINVAL; return(-1); }
  261.  
  262. int __nullopen(char *file,int mode,struct file *f) { return(0); }
  263. int __nullclose(int fd,struct file *f) { return(0); }
  264. int __nullread(int fd,void *data,int nbyte,struct file *f) { return(0); }
  265. int __nullwrite(int fd,void *data,int nbyte,struct file *f) { return(nbyte); }
  266. long __nulllseek(int fd,long lpos,int whence,struct file *f)
  267. { errno = ESPIPE; return(-1); }
  268. int __nullioctl(int fd,int request,void *arg,struct file *f)
  269. { errno = EINVAL; return(-1); }
  270.